<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class RecyclingReport extends Command
{
    private $start_date = '';
    private $end_date = '';

    protected function configure()
    {
        $this->setName('recycling_stats')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->addOption('start_date', null, Option::VALUE_REQUIRED, '开始日期')
            ->addOption('end_date', null, Option::VALUE_REQUIRED, '结束日期')
            ->setDescription('订单回收数据汇总');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {

            $start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : date('Y-m-d', strtotime('-1days'));
            $end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : date('Y-m-d', strtotime('-1days'));

            $this->start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : '';
            $this->end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : '';

            $this->$action($start_date, $end_date);
        }

    }

    /*刷新当日数据*/
    public function daily_channel_today($start_date,$end_date)
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');

        $this->daily_channel($start_date,$end_date);
        $this->daily_channel_media($start_date,$end_date);
        $this->daily_channel_link($start_date,$end_date);
        $this->daily_channel_quick($start_date,$end_date);
        $this->daily_channel_book($start_date,$end_date);
    }

    /*刷新当日数据*/
    public function daily_agent_today($start_date,$end_date)
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');

        $this->daily_agent($start_date,$end_date);
        $this->daily_agent_media($start_date,$end_date);
        $this->daily_agent_link($start_date,$end_date);
        $this->daily_agent_quick($start_date,$end_date);
        $this->daily_agent_book($start_date,$end_date);
    }

    /*回收报表 -- 代理维度*/
    public function daily_agent($start_date,$end_date)
    {
        $this->process_daily_pub($start_date,$end_date,'agent');
    }

    /*回收报表 -- 投放号维度*/
    public function daily_channel($start_date,$end_date)
    {
        $this->process_daily_pub($start_date,$end_date,'channel');
    }

    /*回收报表 -- 投放号下属媒体维度*/
    public function daily_channel_media($start_date,$end_date)
    {
        $this->process_daily($start_date,$end_date,'media','channel');
    }

    /*回收报表 -- 投放号下属快应用维度*/
    public function daily_channel_quick($start_date,$end_date)
    {
        $this->process_daily($start_date,$end_date,'quick','channel');
    }

    /*回收报表 -- 代理下属媒体维度*/
    public function daily_agent_media($start_date,$end_date)
    {
        $this->process_daily($start_date,$end_date,'media','agent');
    }

    /*回收报表 -- 代理下属快应用维度*/
    public function daily_agent_quick($start_date,$end_date)
    {
        $this->process_daily($start_date,$end_date,'quick','agent');
    }

    /* 回收报表多维度统一处理
     * 投放号及代理
     * 媒体、投放号、书籍、推广链接
     */
    private function process_daily($start_date,$end_date,$dimension,$role)
    {
        if ($start_date == $end_date && $end_date != date('Y-m-d') ) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        switch ($dimension)
        {
            case 'media':
                $afieldType = 'advert_media_id';
                break;
            case 'quick':
                $afieldType = 'quick_id';
                break;
            default:
                $afieldType = 'advert_media_id';
        }

        $afield     = $role == 'channel' ? 'channel_id' : 'agent_id';
        $roleId     = Db::table('cms_manager_role')->where('alias',$role)->value('id');
        $accounts   = Db::table('cms_manager_user')->where('role_id',$roleId)->column('id');
        $accounts[] = 0;

        $date_rang = getDateByRange($start_date, $end_date);

        foreach ($date_rang as $date) {

            foreach ($accounts as $account_id)
            {

                $end_date = date('Y-m-d', strtotime('+29 day', strtotime($date)));

                if($dimension == 'quick')
                {
                    $list = Db::table('quick_user')
                        ->where('source_'.$afield, $account_id)
                        ->where('regdate','>=',$date)
                        ->where('regdate','<=',$end_date)
                        ->field( 'source_quick_id as quick_id,count(id) as total_fan_num')
                        ->group('source_'.$afield . ',source_quick_id')->select();
                }else{
                    $list = Db::table('quick_user')
                        ->where('source_'.$afield, $account_id)
                        ->where('regdate','>=',$date)
                        ->where('regdate','<=',$end_date)
                        ->field( 'advert_media_id,count(id) as total_fan_num')
                        ->group('source_'.$afield . ',advert_media_id')->select();
                }

                $data = [];

                foreach ($list as $lv) {

                    if($dimension == 'media') {
                        $afieldTypeVal = $lv['advert_media_id'];
                    }
                    else if($dimension == 'quick')
                    {
                        $afieldTypeVal = $lv['quick_id'];
                    }

                    if($dimension == 'quick')
                    {
                        $total_fan_num = 0; /*Db::table('quick_user')
                            ->where('regdate','<=',$date)
                            ->where('quick_id', $lv['quick_id'])
                            ->where('source_'.$afield, $account_id)
                            ->count('id');*/

                        $total_fan_num_shielded_area = 0;/*Db::table('quick_user')
                            ->where('regdate','<=',$date)
                            ->where('quick_id', $lv['quick_id'])
                            ->where('source_'.$afield, $account_id)
                            ->where('ip_blocked', 1)
                            ->count('id');*/

                        $new_fan_num = Db::table('quick_user')
                            ->where('source_quick_id', $lv['quick_id'])
                            ->where('source_'.$afield, $account_id)
                            ->where('regdate', $date)
                            ->count('id');

                        $link_ids = Db::table('quick_link')->where($afield, $account_id)->where('quick_id',$lv['quick_id'])->column('id');
                    }else{
                        $total_fan_num = 0;/*Db::table('quick_user')
                            ->where('regdate','<=',$date)
                            ->where('source_link_id', $lv['source_link_id'])
                            ->count('id');*/

                        $total_fan_num_shielded_area = 0;/*Db::table('quick_user')
                            ->where('regdate','<=',$date)
                            ->where('source_link_id', $lv['source_link_id'])
                            ->where('ip_blocked', 1)
                            ->count('id');*/

                        $new_fan_num = Db::table('quick_user')
                            ->where('advert_media_id', $lv['advert_media_id'])
                            ->where('regdate', $date)
                            ->count('id');

                    }

                    if($dimension == 'media') {

                        $new_fan_recharge = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('user_reg_date', $date)
                            ->where('status', 2)
                            ->where('create_date', $date)
                            ->sum('total_fee');

                        $pay_new_fan_num = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('user_reg_date', $date)
                            ->where('recharge_times', 1)
                            ->where('status', 2)
                            ->where('create_date', $date)->count('id');

                        $first_order_num = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('status', 2)
                            ->where('create_date', $date)->count('id');

                        $pay_total_fan_num = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('user_reg_date','>=',$date)
                            ->where('user_reg_date','<=',$end_date)
                            ->where('status', 2)->group('uid')->count('id');

                        $total_cost = Db::table('rp_cost_manual')
                            ->where($afield, $account_id)
                            ->where('advert_media_id', '=', $afieldTypeVal)
                            ->where('type', 2)
                            ->where('cost_date', $date)
                            ->sum('money');

                        $first_annual_recharge = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('status', 2)
                            ->where('type', 2)
                            ->where('vipdays', '>=', 365)
                            ->where('create_date', $date)->sum('total_fee');

                        $first_order_annual_num = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('status', 2)
                            ->where('type', 2)
                            ->where('vipdays', '>=', 365)
                            ->where('create_date', $date)->count('id');

                        $first_accumulated_recharge = Db::table('pay_order_success')
                            ->where('advert_media_id', '=', $lv['advert_media_id'])
                            ->where('status', 2)
                            ->where('create_date', $date)->sum('total_fee');

                    }else{
                        $new_fan_recharge = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('user_reg_date', $date)
                            ->where('status', 2)
                            ->where('create_date', $date)
                            ->sum('total_fee');

                        $pay_new_fan_num = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('user_reg_date', $date)
                            ->where('recharge_times', 1)
                            ->where('status', 2)
                            ->where('create_date', $date)->count('id');

                        $first_order_num = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('status', 2)
                            ->where('create_date', $date)->count('id');

                        $pay_total_fan_num = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('user_reg_date','>=',$date)
                            ->where('user_reg_date','<=',$end_date)
                            ->where('status', 2)->group('uid')->count('id');

                        $total_cost = Db::table('rp_cost_manual')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('type',3)
                            ->where('cost_date', $date)
                            ->sum('money');

                        $first_annual_recharge = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('status', 2)
                            ->where('type', 2)
                            ->where('vipdays', '>=', 365)
                            ->where('create_date', $date)->sum('total_fee');

                        $first_order_annual_num = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('status', 2)
                            ->where('type', 2)
                            ->where('vipdays', '>=', 365)
                            ->where('create_date', $date)->count('id');

                        $first_accumulated_recharge = Db::table('pay_order_success')
                            ->where('quick_id', '=', $lv['quick_id'])
                            ->where('status', 2)
                            ->where('create_date', $date)->sum('total_fee');
                    }


                    if(isset($data[$afieldTypeVal]['total_fan_num']))
                    {
                        $data[$afieldTypeVal]['total_fan_num'] = $total_fan_num+$data[$afieldTypeVal]['total_fan_num'];
                    }else{
                        $data[$afieldTypeVal]['total_fan_num'] = $total_fan_num;
                    }

                    if(isset($data[$afieldTypeVal]['total_fan_num_shielded_area']))
                    {
                        $data[$afieldTypeVal]['total_fan_num_shielded_area'] = $total_fan_num_shielded_area+$data[$afieldTypeVal]['total_fan_num_shielded_area'];
                    }else{
                        $data[$afieldTypeVal]['total_fan_num_shielded_area'] = $total_fan_num_shielded_area;
                    }

                    if(isset($data[$afieldTypeVal]['new_fan_recharge']))
                    {
                        $data[$afieldTypeVal]['new_fan_recharge'] = $new_fan_recharge+$data[$afieldTypeVal]['new_fan_recharge'];
                    }else{
                        $data[$afieldTypeVal]['new_fan_recharge'] = $new_fan_recharge;
                    }

                    if(isset($data[$afieldTypeVal]['new_fan_num']))
                    {
                        $data[$afieldTypeVal]['new_fan_num'] = $new_fan_num + $data[$afieldTypeVal]['new_fan_num'];
                    }else{
                        $data[$afieldTypeVal]['new_fan_num'] = $new_fan_num;
                    }

                    if(isset($data[$afieldTypeVal]['pay_new_fan_num']))
                    {
                        $data[$afieldTypeVal]['pay_new_fan_num'] = $pay_new_fan_num+$data[$afieldTypeVal]['pay_new_fan_num'];
                    }else{
                        $data[$afieldTypeVal]['pay_new_fan_num'] = $pay_new_fan_num;
                    }

                    if(isset($data[$afieldTypeVal]['first_order_num']))
                    {
                        $data[$afieldTypeVal]['first_order_num'] = $first_order_num+$data[$afieldTypeVal]['first_order_num'];
                    }else{
                        $data[$afieldTypeVal]['first_order_num'] = $first_order_num;
                    }

                    if(isset($data[$afieldTypeVal]['pay_total_fan_num']))
                    {
                        $data[$afieldTypeVal]['pay_total_fan_num'] = $pay_total_fan_num+$data[$afieldTypeVal]['pay_total_fan_num'];
                    }else{
                        $data[$afieldTypeVal]['pay_total_fan_num'] = $pay_total_fan_num;
                    }

                    if(isset($data[$afieldTypeVal]['total_cost']))
                    {
                        $data[$afieldTypeVal]['total_cost'] = $total_cost+$data[$afieldTypeVal]['total_cost'];
                    }else{
                        $data[$afieldTypeVal]['total_cost'] = $total_cost;
                    }

                    if(isset($data[$afieldTypeVal]['first_annual_recharge']))
                    {
                        $data[$afieldTypeVal]['first_annual_recharge'] = $first_annual_recharge+$data[$afieldTypeVal]['first_annual_recharge'];
                    }else{
                        $data[$afieldTypeVal]['first_annual_recharge'] = $first_annual_recharge;
                    }

                    if(isset($data[$afieldTypeVal]['first_order_annual_num']))
                    {
                        $data[$afieldTypeVal]['first_order_annual_num'] = $first_order_annual_num+$data[$afieldTypeVal]['first_order_annual_num'];
                    }else{
                        $data[$afieldTypeVal]['first_order_annual_num'] = $first_order_annual_num;
                    }

                    if(isset($data[$afieldTypeVal]['first_accumulated_recharge']))
                    {
                        $data[$afieldTypeVal]['first_accumulated_recharge'] = $first_accumulated_recharge+$data[$afieldTypeVal]['first_accumulated_recharge'];
                    }else{
                        $data[$afieldTypeVal]['first_accumulated_recharge'] = $first_accumulated_recharge;
                    }

                }

                foreach ($data as $k=>$dv)
                {
                    $record = Db::table('rp_recycling_report_daily_'.$role.'_' . $dimension)
                        ->where([
                            'report_date' => $date, $afieldType => $k, $afield => $account_id
                        ])->field('id')->find();

                    if ($record) {

                        Db::table('rp_recycling_report_daily_'.$role.'_' . $dimension)->where(['id' => $record['id']])->update([
                            'new_fan_recharge'          => $dv['new_fan_recharge'],
                            'total_cost'                => $dv['total_cost'],
                            'first_accumulated_recharge' => $dv['first_accumulated_recharge'],
                            'first_consumption_amount'  => $dv['total_cost'],
                            'new_fan_num'               => $dv['new_fan_num'],
                            'pay_new_fan_num'           => $dv['pay_new_fan_num'],
                            'total_fan_num'             => $dv['total_fan_num'],
                            'total_fan_num_shielded_area' => $dv['total_fan_num_shielded_area'],
                            'pay_total_fan_num'         => $dv['pay_total_fan_num'],
                            'first_order_num'           => $dv['first_order_num'],
                            'first_annual_recharge'     => $dv['first_annual_recharge'],
                            'first_order_annual_num'    => $dv['first_order_annual_num']
                        ]);

                    } else {

                        Db::table('rp_recycling_report_daily_'.$role.'_' . $dimension)->insert([
                            'new_fan_recharge'          => $dv['new_fan_recharge'],
                            'total_cost'                => $dv['total_cost'],
                            'first_accumulated_recharge' => $dv['first_accumulated_recharge'],
                            'first_consumption_amount'  => $dv['total_cost'],
                            'new_fan_num'               => $dv['new_fan_num'],
                            'pay_new_fan_num'           => $dv['pay_new_fan_num'],
                            'total_fan_num'             => $dv['total_fan_num'],
                            'total_fan_num_shielded_area' => $dv['total_fan_num_shielded_area'],
                            'pay_total_fan_num'         => $dv['pay_total_fan_num'],
                            'first_order_num'           => $dv['first_order_num'],
                            'first_annual_recharge'     => $dv['first_annual_recharge'],
                            'first_order_annual_num'    => $dv['first_order_annual_num'],
                            $afield                     => $account_id,
                            'report_date'               => $date,
                            $afieldType                 => $k,
                        ]);
                    }
                }

            }


        }

       if ($this->start_date && $this->end_date) {
            $end_date_t = $this->end_date;
            $start_date_t = $this->start_date;
        } else {
            $end_date_t = date('Y-m-d');
            $start_date_t = date('Y-m-d', strtotime('-30days'));
        }

        $list_t = Db::table('rp_recycling_report_daily_'.$role.'_' . $dimension)
            ->where('report_date','>=',$start_date_t)
            ->where('report_date','<=',$end_date_t)
            ->field('id,'.$afieldType.','.$afield.',report_date')
            ->select();

        foreach ($list_t as $lv)
        {
            $this->updateNewFanRecharge($lv,$lv['report_date'],$dimension,$role,$afieldType,$afield);
        }

    }

    /* 回收报表多维度统一处理
     * 投放号、代理
     */
    private function process_daily_pub($start_date,$end_date,$role)
    {
        if ($start_date == $end_date && $end_date != date('Y-m-d') ) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }

        $afield     = $role == 'channel' ? 'channel_id' : 'agent_id';
        $roleId     = Db::table('cms_manager_role')->where('alias',$role)->value('id');
        $accounts   = Db::table('cms_manager_user')->where('role_id',$roleId)->column('id');
        $accounts[] = 0;

        $date_rang = getDateByRange($start_date, $end_date);

        foreach ($date_rang as $date) {

            foreach ($accounts as $account_id)
            {

                $end_date = date('Y-m-d', strtotime('+29 day', strtotime($date)));

                $total_fan_num = 0;/*Db::table('quick_user')
                    ->where('source_'.$afield, $account_id)
                    ->where('regdate','<=',$date)
                    ->count( 'id');*/

                $total_fan_num_shielded_area = 0;/*Db::table('quick_user')
                    ->where('regdate','<=',$date)
                    ->where('source_'.$afield, $account_id)
                    ->where('ip_blocked', 1)
                    ->count('id');*/

                $new_fan_recharge = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('user_reg_date', $date)
                    ->where('status', 2)
                    ->where('create_date', $date)->sum('total_fee');

                $new_fan_num = Db::table('quick_user')
                    ->where('source_'.$afield, $account_id)
                    ->where('regdate', $date)
                    ->count('id');

                $pay_new_fan_num = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('user_reg_date', $date)
                    ->where('recharge_times', 1)
                    ->where('status', 2)
                    ->where('create_date', $date)->count('id');

                $first_order_num = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('status', 2)
                    ->where('create_date', $date)->count('id');

                $pay_total_fan_num = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('user_reg_date','>=',$date)
                    ->where('user_reg_date','<=',$end_date)
                    ->where('status', 2)->group('uid')->count('id');

                $total_cost = Db::table('rp_cost_manual')
                    ->where($afield, $account_id)
                    ->where('type',1)
                    ->where('cost_date', $date)
                    ->sum('money');

                $first_annual_recharge = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('status', 2)
                    ->where('type', 2)
                    ->where('vipdays', '>=', 365)
                    ->where('create_date', $date)->sum('total_fee');

                $first_order_annual_num = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('status', 2)
                    ->where('type', 2)
                    ->where('vipdays', '>=', 365)
                    ->where('create_date', $date)->count('id');

                $first_accumulated_recharge = Db::table('pay_order_success')
                    ->where($afield, $account_id)
                    ->where('status', 2)
                    ->where('create_date', $date)->sum('total_fee');

                $record = Db::table('rp_recycling_report_daily_'.$role)
                    ->where([
                        'report_date' => $date, $afield => $account_id
                    ])->field('id')->find();

                if ($record) {

                    Db::table('rp_recycling_report_daily_'.$role)->where(['id' => $record['id']])->update([
                        'new_fan_recharge'          => $new_fan_recharge,
                        'total_cost'                => $total_cost,
                        'first_accumulated_recharge' => $first_accumulated_recharge,
                        'first_consumption_amount'  => $total_cost,
                        'new_fan_num'               => $new_fan_num,
                        'pay_new_fan_num'           => $pay_new_fan_num,
                        'total_fan_num'             => $total_fan_num,
                        'total_fan_num_shielded_area' => $total_fan_num_shielded_area,
                        'pay_total_fan_num'         => $pay_total_fan_num,
                        'first_order_num'           => $first_order_num,
                        'first_annual_recharge'     => $first_annual_recharge,
                        'first_order_annual_num'    => $first_order_annual_num
                    ]);

                } else {

                    Db::table('rp_recycling_report_daily_'.$role)->insert([
                        'new_fan_recharge'          => $new_fan_recharge,
                        'total_cost'                => $total_cost,
                        'first_accumulated_recharge' => $first_accumulated_recharge,
                        'first_consumption_amount'  => $total_cost,
                        'new_fan_num'               => $new_fan_num,
                        'pay_new_fan_num'           => $pay_new_fan_num,
                        'total_fan_num'             => $total_fan_num,
                        'total_fan_num_shielded_area' => $total_fan_num_shielded_area,
                        'pay_total_fan_num'         => $pay_total_fan_num,
                        'first_order_num'           => $first_order_num,
                        'first_annual_recharge'     => $first_annual_recharge,
                        'first_order_annual_num'    => $first_order_annual_num,
                        $afield                     => $account_id,
                        'report_date'               => $date,
                    ]);
                }

            }


        }

        if ($this->start_date && $this->end_date) {
            $end_date_t = $this->end_date;
            $start_date_t = $this->start_date;
        } else {
            $end_date_t = date('Y-m-d');
            $start_date_t = date('Y-m-d', strtotime('-30days'));
        }

        $list_t = Db::table('rp_recycling_report_daily_'.$role)
            ->where('report_date','>=',$start_date_t)
            ->where('report_date','<=',$end_date_t)
            ->field('id,'.$afield.',report_date')
            ->select();

        foreach ($list_t as $lv)
        {
            $this->updateNewFanRecharge($lv,$lv['report_date'],'',$role,'',$afield);
        }

    }

    /*更新指定日期新用户充值*/
    private function updateNewFanRecharge($lv,$report_date,$dimension,$role,$afieldType,$afield)
    {
        $report_date = date('Y-m-d', strtotime($report_date));
        $end_date = date('Y-m-d', strtotime('+29 day', strtotime($report_date)));

        $date_rang = getDateByRange($report_date, $end_date);

        $data = $this->getT30RangeNewFanRecharge($lv,$date_rang,$end_date,$afieldType,$afield);

        if(strlen($dimension) < 1)
        {
            Db::table('rp_recycling_report_daily_'.$role)->where('id',$lv['id'])->update($data);
        }else{
            Db::table('rp_recycling_report_daily_'.$role.'_' . $dimension)->where('id',$lv['id'])->update($data);
        }

    }

    /*获取t2-t30内当日日期用户后续充值*/
    private function getT30RangeNewFanRecharge($lv,$date_rang,$end_date,$afieldType,$afield)
    {
        $data = [];
        $i = 1;
        foreach ($date_rang as $date)
        {

            if(strlen($afieldType) > 1)
            {
                $data['t'.$i] = Db::table('pay_order_success')
                    ->where($afieldType,$lv[$afieldType])
                    ->where($afield,$lv[$afield])
                    ->where('user_reg_date',$lv['report_date'])
                    ->where('status',2)
                    ->where('create_date',$date)->sum('total_fee');
            }else{
                $data['t'.$i] = Db::table('pay_order_success')
                    ->where($afield,$lv[$afield])
                    ->where('user_reg_date',$lv['report_date'])
                    ->where('status',2)
                    ->where('create_date',$date)->sum('total_fee');
            }


            $i++;

        }


        if(strlen($afieldType) > 1)
        {

            $data['accumulated_recharge'] = Db::table('pay_order_success')
                ->where($afieldType,$lv[$afieldType])
                ->where($afield,$lv[$afield])
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)
                ->sum('total_fee');

            $data['accumulated_order_num'] = Db::table('pay_order_success')
                ->where($afieldType,$lv[$afieldType])
                ->where($afield,$lv[$afield])
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)
                ->count('id');


            if($afieldType == 'advert_media_id') {
                $data['accumulated_total_cost'] = Db::table('rp_cost_manual')
                    ->where($afieldType, $lv[$afieldType])
                    ->where($afield, $lv[$afield])
                    ->where('type', 2)
                    ->where('cost_date', '>=', $lv['report_date'])
                    ->where('cost_date', '<=', $end_date)
                    ->sum('money');
            }else if($afieldType == 'quick_id'){
                $data['accumulated_total_cost'] = Db::table('rp_cost_manual')
                    ->where($afieldType,$lv[$afieldType])
                    ->where($afield,$lv[$afield])
                    ->where('type',1)
                    ->where('cost_date','>=',$lv['report_date'])
                    ->where('cost_date','<=',$end_date)
                    ->sum('money');
            }else{
                $data['accumulated_total_cost'] = Db::table('rp_cost_manual')
                    ->where($afieldType,$lv[$afieldType])
                    ->where($afield,$lv[$afield])
                    ->where('type',3)
                    ->where('cost_date','>=',$lv['report_date'])
                    ->where('cost_date','<=',$end_date)
                    ->sum('money');
            }

        }else{

            $data['accumulated_recharge'] = Db::table('pay_order_success')
                ->where($afield,$lv[$afield])
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)
                ->sum('total_fee');

            $data['accumulated_order_num'] = Db::table('pay_order_success')
                ->where($afield,$lv[$afield])
                ->where('create_date','<=', $end_date)
                ->where('create_date','>=', $lv['report_date'])
                ->where('user_reg_date',$lv['report_date'])
                ->where('status',2)
                ->count('id');

            $data['accumulated_total_cost'] = Db::table('rp_cost_manual')
                ->where($afield,$lv[$afield])
                ->where('type',1)
                ->where('cost_date','>=',$lv['report_date'])
                ->where('cost_date','<=',$end_date)
                ->sum('money');
        }
        
        unset($data['t1']);
        return $data;
    }

}